package com.riven.bilibilidemo.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.BannerActivity;
import com.riven.bilibilidemo.adapter.MyAdapter;
import com.riven.bilibilidemo.adapter.MyRecyAdapter;
import com.riven.bilibilidemo.application.GlideImageLoader;
import com.riven.bilibilidemo.bean.BangumiBean;
import com.riven.bilibilidemo.bean.BangumiBeanTwo;
import com.riven.bilibilidemo.utils.OkHttpManager;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Request;

/**
 * Created by Pooh on 2017/3/9.
 */
/*
*@date 创建时间 2017年3月10日07:56:06
*@作者：孙亚东
*@作用：番剧页面
*/


public class BangumiFragment extends BaseFragment {

    private RecyclerView mRecycle;
    private MyAdapter myAdapter;
    private Banner banner;
    private String path="http://bangumi.bilibili.com/api/app_index_page_v4?build=3940&device=phone&mobi_app=iphone&platform=ios";
    private String pathUrl="http://bangumi.bilibili.com/api/bangumi_recommend?access_key=f5bd4e793b82fba5aaf5b91fb549910a&actionKey=appkey&appkey=27eb53fc9058f8c3&build=3470&cursor=0&device=phone&mobi_app=iphone&pagesize=10&platform=ios&sign=56329a5709c401d4d7c0237f64f7943f&ts=1469613558";
    ArrayList<String> list = new ArrayList<>();
    private BangumiBean bean;
    private RecyclerView recycle_bangumi2;
    private BangumiBeanTwo bangumiBeanTwo;
    //声明哪一次请求
    public final int firstData=0;
    public final int twoData=1;
    private SwipeRefreshLayout swipe;

    @Override
    public int layoutID() {
        return R.layout.fragment_bangumi;
    }

    @Override
    protected void initViews(View view) {
        mRecycle = (RecyclerView) view.findViewById(R.id.recycle_bangumi1);
        recycle_bangumi2 = (RecyclerView) view.findViewById(R.id.recycle_bangumi2);
        banner = (Banner) view.findViewById(R.id.banner);
        swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
        //图标颜色
        swipe.setColorSchemeColors(Color.parseColor("#FF4081"));
        //下拉刷新
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
               new  Handler().postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       swipe.setRefreshing(false);
                   }
               },300);
            }
        });
    }

    @Override
    protected void initData() {
        getDataFromNet(twoData,pathUrl);
        getDataFromNet(firstData,path);
    }

    //将两次请求网络抽成一个方法
    //参数一：标记是哪一个请求
    //参数二：请求数据的路径
    public void getDataFromNet(final int tag, String path){
        OkHttpManager.getAsync(path, new OkHttpManager.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {
                Toast.makeText(getActivity(),"请求出错...",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void requestSuccess(String result) throws Exception {
                //判断是哪一次请求
                Gson gson = new Gson();
                if(tag==firstData){
                    bean = gson.fromJson(result, BangumiBean.class);
                    list.add(bean.getResult().getAd().getHead().get(0).getImg());
                    list.add(bean.getResult().getAd().getHead().get(1).getImg());
                    list.add(bean.getResult().getAd().getHead().get(2).getImg());
                    list.add(bean.getResult().getAd().getHead().get(3).getImg());
                    banner.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    banner.setImages(list);
                    //banner设置方法全部调用完毕时最后调用
                    banner.start();
                    banner.setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(int position) {
                            Intent intent = new Intent(getActivity(), BannerActivity.class);
                            intent.putExtra("url",bean.getResult().getAd().getHead().get(position).getLink());
                            getActivity().startActivity(intent);
                        }
                    });
                    //解决ScollView嵌套RecyclerView界面卡顿问题
                    mRecycle.setLayoutManager(new GridLayoutManager(getActivity(),1){
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    myAdapter = new MyAdapter(getActivity(),bean);
                    mRecycle.setAdapter(myAdapter);
                }else{
                    bangumiBeanTwo = gson.fromJson(result, BangumiBeanTwo.class);
                    //解决ScollView嵌套RecyclerView界面卡顿问题
                    recycle_bangumi2.setLayoutManager(new LinearLayoutManager(getActivity()){
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    recycle_bangumi2.setAdapter(new MyRecyAdapter(getActivity(),bangumiBeanTwo));
                }
            }
        });
    }

}
